package com.wtone.platform.gat.images.gat.digest;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @Author: aiLin
 * @Date: 2020/9/27 15:57
 * @desc: Http Digest Request contains POST、GET、PUT 测试
 */
@Slf4j
public class HttpRequestUtilsTest {

    private static final Logger logger = LoggerFactory.getLogger(HttpRequestUtilsTest.class);

    private static final String PARAM = "";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin123";

    @Test
    public void testSendGet() {
        String getUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications";
        // String s = HttpRequestUtils.sendGet(getUrl, PARAM, USERNAME, PASSWORD, null);
        String s = HttpRequestUtils.sendGet(getUrl, PARAM, USERNAME, PASSWORD, "json");
        logger.info(s);
        Assert.assertNotNull(s);
    }

    @Test
    public void testSendDelete() {
        String deleteUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/users/test6";
        //     String s = sendDelete(deleteUrl, param, username, password, null, null);
        //     String s = HttpRequestUtils.sendDelete(deleteUrl, PARAM, USERNAME, PASSWORD, null,"json");
        String s = HttpRequestUtils.sendDelete(deleteUrl, PARAM, USERNAME, PASSWORD, null,null);
        logger.info(s);
        Assert.assertNotNull(s);
    }

    @Test
    public void testSendPost() {
        String postUrl2 = "http://127.0.0.1:8066/test";
        String postJson2 = "{ \"password\": \"123\", \"name\": \"javaboy\", \"serverName\": \"_defaultServer_\", \"description\": \"test\", \"saveFieldList\": [ \"\" ], \"version\": \"v1.0\" }";
        // 返回json
        String s = HttpRequestUtils.sendPost(postUrl2, PARAM, USERNAME, PASSWORD, postJson2, "json","");
        // 返回xml
        // String s = HttpRequestUtils.sendPost(postUrl2, PARAM, USERNAME, PASSWORD, postJson2, null);
        logger.info(s);
        Assert.assertNotNull(s);

        try {
            String x = new String(Base64.getDecoder().decode("MTYwMTIwMTMwMDk4Nzo4MGY3OWI5YjQyNGNlNDRjYmE3YzMxYmU3NDQ5Y2EzYQ=="), "UTF-8");
            log.info(x);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSendPost2() {
        String postUrl2 = "http://127.0.0.1:8066/VIID/System/Keepalive";
        String postJson2 = "{\n" +
                "\t\"KeepaliveObject\": {\n" +
                "\t\t\"DeviceID\": 31000051511201250000\n" +
                "\t}\n" +
                "}";
        // 返回json
        String s = HttpRequestUtils.sendPost(postUrl2, PARAM, USERNAME, PASSWORD, postJson2, "json","31000051511201250000");
        // 返回xml
        // String s = HttpRequestUtils.sendPost(postUrl2, PARAM, USERNAME, PASSWORD, postJson2, null);
        logger.info(s);
        Assert.assertNotNull(s);
    }



    @Test
    public void testSendPUT() {
        String putUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/Relay/streamfiles/6D07D7E7623B95889C33DC5901307461_0/actions/connect";
        String putJson = "{ \"vhost\":\"_defaultVHost_\", \"mediaCasterType\":\"liverepeater\" }";
        // String s = HttpRequestUtils.sendPUT(putUrl, PARAM, USERNAME, PASSWORD, putJson, "json");
        String s = HttpRequestUtils.sendPUT(putUrl, PARAM, USERNAME, PASSWORD, putJson, null);
        logger.info(s);
        Assert.assertNotNull(s);
    }

}

